package service.securite;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.dossier.Dossier;


public class DroitsAcces {

	/* CLIENT */
	private final static int TC_INVITE = 0;
	private final static int TC_CLIENTSTANDARD = 1;
	private final static int TC_COMPTEDESERVICE = 2; 
	
	/* ADMINISTRATEUR */
	private final static String TC_SUPERADMIN = "typeSuperAdmin";
	private final static String TC_SERVICEACHATDFC = "typeServiceAchatDFC";
	private final static String TC_SERVICEBUDGETDFC = "typeServiceBudgetDFC";
	private final static String TC_ADMINPRODUIT = "typeAdminProduit";
	private final static String TC_ADMINCOMMANDE = "typeAdminCommande";
	private final static String TC_ADMINCOMPTE = "typeAdminCompte";
	private final static String TC_ADMINSTOCK = "typeAdminStock";
	private final static String TC_FOURNISSEUR = "typeFournisseur";
	private final static String TC_ADMIN = "typeAdmin";
	
	
	public static String getTcSuperadmin() {
		// TODO Auto-generated method stub
		return TC_SUPERADMIN;
	}
	
	public static String getTcAdmin() {
		return TC_ADMIN;
	}
	
	
	public static String getTcAdmincompte() {
		return TC_ADMINCOMPTE;
	}

	public static void setInvite(HttpSession session) {
		if (session == null)
			return;
		
		session.setAttribute("typeCompte", DroitsAcces.TC_INVITE);
	}

	/* CLIENT */
	public static boolean isConnecte(HttpServletRequest request) {
		if (request == null)
			return false;
		
		HttpSession session = request.getSession();
		if (session == null)
			return false;

		Object attribut = session.getAttribute("typeCompte");
		if (attribut != null && (Integer)attribut != DroitsAcces.TC_INVITE)
			return true;
		
		return false;
	}

	public static boolean isConnecteClientStandard(HttpServletRequest request) {
		if (request == null)
			return false;
		
		HttpSession session = request.getSession();
		if (session == null)
			return false;

		Object attribut = session.getAttribute("typeCompte");
		if (attribut != null && (Integer)attribut == DroitsAcces.TC_CLIENTSTANDARD)
			return true;
		
		return false;
	}
	
	public static void setClientStandard(HttpSession session) {
		if (session == null)
			return;
		
		session.setAttribute("typeCompte", DroitsAcces.TC_CLIENTSTANDARD);
	}

	public static boolean isConnecteCompteDeService(HttpServletRequest request) {
		if (request == null)
			return false;
		
		HttpSession session = request.getSession();
		if (session == null)
			return false;

		Object attribut = session.getAttribute("typeCompte");
		if (attribut != null && (Integer)attribut == DroitsAcces.TC_COMPTEDESERVICE)
			return true;
		
		return false;
	}
	
	public static void setCompteDeService(HttpSession session) {
		if (session == null)
			return;
		
		session.setAttribute("typeCompte", DroitsAcces.TC_COMPTEDESERVICE);
	}
	
	public boolean isClient(HttpServletRequest request) {
		if (request == null)
			return false;
		
		if (DroitsAcces.isConnecteClientStandard(request) || DroitsAcces.isConnecteCompteDeService(request))
			return true;
		
		return false;
	}
	
	/* ADMINISTRATEUR */
	public static boolean isAdmin(HttpServletRequest request) {
			
		
		if (request == null)
		{
			return false;
		}
		
		HttpSession session = request.getSession();
				
		if (session == null)
		{
			return false;
		}
			
		
		Object objIdCompteSpecial = session.getAttribute("id_comptespecial");
		if (objIdCompteSpecial == null || objIdCompteSpecial.equals(""))
		{
			return false;
		}
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
		{
			return false;
		}
		
		for (int i = 0; i < tabC.length; i++)
		{
			//System.out.println("cookie" + tabC[i].getName());
			if (tabC[i].getName().equals(DroitsAcces.TC_ADMIN))
			{
				return true;
			}
			
		}
		return false;
	}
	
	private static void setAdmin(HttpServletResponse reponse) {
		Cookie c = new Cookie(DroitsAcces.TC_ADMIN, "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
	}
	
	public static boolean isSuperAdmin(HttpServletRequest request) {
		if (request == null)
			return false;
		
		if (!DroitsAcces.isAdmin(request))
			return false;
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			return false;
		
		for (int i = 0; i < tabC.length; i++)
			if (tabC[i].getName().equals(DroitsAcces.TC_SUPERADMIN))
				return true;
		
		return false;
	}
	
	public static void setSuperAdmin(HttpServletResponse reponse) {
		if (reponse == null)
			return;
		
		Cookie c = new Cookie(DroitsAcces.TC_SUPERADMIN, "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
		
		DroitsAcces.setAdmin(reponse);
	}

	public static boolean isAdminCompte(HttpServletRequest request) {
		if (request == null)
		{
			return false;
		}
		if (!DroitsAcces.isAdmin(request))
		{
			return false;
		}
		
		if (DroitsAcces.isSuperAdmin(request))
		{
			return true;
		}
			
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			{
			return false;
			}
		
		for (int i = 0; i < tabC.length; i++)
			{
				if (tabC[i].getName().equals(DroitsAcces.getTcAdmincompte()))
				{
					return true;
				}
			}
				
		
		return false;
	}
	
	public static void setAdminCompte(HttpServletResponse reponse) {
		
		if (reponse == null)
			return;
		
		Cookie c = new Cookie(DroitsAcces.getTcAdmincompte(), "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
		
		DroitsAcces.setAdmin(reponse);
		
	}
	
	public static boolean isAdminStock(HttpServletRequest request) {
		if (request == null)
			return false;
		
		if (!DroitsAcces.isAdmin(request))
			return false;
		
		if (DroitsAcces.isSuperAdmin(request))
			return true;
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			return false;
		
		for (int i = 0; i < tabC.length; i++)
			if (tabC[i].getName().equals(DroitsAcces.getTcAdminstock()))
				return true;
		
		return false;
	}
	
	public static void setAdminStock(HttpServletResponse reponse) {
		if (reponse == null)
			return;
		
		Cookie c = new Cookie(DroitsAcces.getTcAdminstock(), "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
		
		DroitsAcces.setAdmin(reponse);
	}
	
	public static boolean isServiceAchatDFC(HttpServletRequest request) {
		if (request == null)
			return false;
		
		if (!DroitsAcces.isAdmin(request))
			return false;
		
		if (DroitsAcces.isSuperAdmin(request))
			return true;
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			return false;
		
		for (int i = 0; i < tabC.length; i++)
			if (tabC[i].getName().equals(DroitsAcces.getTcServiceachatdfc()))
				return true;
		
		return false;
	}
	
	public static void setServiceAchatDFC(HttpServletResponse reponse) {
		if (reponse == null)
			return;
		
		Cookie c = new Cookie(DroitsAcces.getTcServiceachatdfc(), "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
		
		DroitsAcces.setAdmin(reponse);
	}

	public static boolean isServiceBudgetDFC(HttpServletRequest request) {
		if (request == null)
			return false;
		
		if (!DroitsAcces.isAdmin(request))
			return false;
		
		if (DroitsAcces.isSuperAdmin(request))
			return true;
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			return false;
		
		for (int i = 0; i < tabC.length; i++)
			if (tabC[i].getName().equals(DroitsAcces.getTcServicebudgetdfc()))
				return true;
		
		return false;
	}
	
	public static void setServiceBudgetDFC(HttpServletResponse reponse) {
		if (reponse == null)
			return;
		
		Cookie c = new Cookie(DroitsAcces.getTcServicebudgetdfc(), "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
		
		DroitsAcces.setAdmin(reponse);
	}

	public static boolean isAdminProduit(HttpServletRequest request) {
		if (request == null)
			return false;
		
		if (!DroitsAcces.isAdmin(request))
			return false;
		
		if (DroitsAcces.isSuperAdmin(request))
			return true;
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			return false;
		
		for (int i = 0; i < tabC.length; i++)
			if (tabC[i].getName().equals(DroitsAcces.getTcAdminproduit()))
				return true;
		
		return false;
	}
	
	public static void setAdminProduit(HttpServletResponse reponse) {
		if (reponse == null)
			return;
		
		Cookie c = new Cookie(DroitsAcces.getTcAdminproduit(), "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
		
		DroitsAcces.setAdmin(reponse);
	}

	public static boolean isAdminCommande(HttpServletRequest request) {
		if (request == null)
			return false;
		
		if (!DroitsAcces.isAdmin(request))
			return false;
		
		if (DroitsAcces.isSuperAdmin(request))
			return true;
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			return false;
		
		
		for (int i = 0; i < tabC.length; i++)
			if (tabC[i].getName().equals(DroitsAcces.getTcAdmincommande()))
				return true;
		
		
		return false;
	}
	
	public static void setAdminCommande(HttpServletResponse reponse) {
		if (reponse == null)
			return;
		
		Cookie c = new Cookie(DroitsAcces.getTcAdmincommande(), "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
		
		DroitsAcces.setAdmin(reponse);
	}

	public static boolean isFournisseur(HttpServletRequest request) {
		if (request == null)
			return false;
		
		if (!DroitsAcces.isAdmin(request))
			return false;
		
		if (DroitsAcces.isSuperAdmin(request))
			return true;
		
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			return false;
		
		for (int i = 0; i < tabC.length; i++)
			if (tabC[i].getName().equals(DroitsAcces.getTcFournisseur()))
				return true;
		
		return false;
	}
	
	public static void setFournisseur(HttpServletResponse reponse) {
		
		if (reponse == null)
			return;
		
		Cookie c = new Cookie(DroitsAcces.getTcFournisseur(), "");
		c.setPath("/" + Dossier.getDossierAdmin());
		reponse.addCookie(c);
		
		DroitsAcces.setAdmin(reponse);
	}
	
	public static boolean deconnexionAdmin(HttpServletRequest request, HttpServletResponse response) {
		Cookie[] tabC = request.getCookies();
		if (tabC == null)
			return true;
		
		for (Cookie c : tabC) { 
			c.setMaxAge(0);
			response.addCookie(c);
		}
		
		return true;	
	}
	
	public static HttpServletResponse test(HttpServletResponse reponse)
	{
        Cookie ck=new Cookie("name2","Java Struts");
        reponse.addCookie(ck);
        
        return reponse;
	}

	public static String getTcAdminproduit() {
		return TC_ADMINPRODUIT;
	}

	public static String getTcAdmincommande() {
		return TC_ADMINCOMMANDE;
	}

	public static String getTcFournisseur() {
		return TC_FOURNISSEUR;
	}

	public static String getTcServiceachatdfc() {
		return TC_SERVICEACHATDFC;
	}

	public static String getTcServicebudgetdfc() {
		return TC_SERVICEBUDGETDFC;
	}

	public static String getTcAdminstock() {
		return TC_ADMINSTOCK;
	}

}
